/*
 * Project: yygh-parent
 *
 * File Created at 2023/7/8
 *
 * 自定义时间格式代替DATE标签：2023-07-08 18:33:18:33
 */
package com.atguigu.yygh.order.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.yygh.common.exception.HospitalException;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.listener.DictListener;
import com.atguigu.yygh.order.mapper.DictMapper;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.model.cmn.DictEeVo;
import com.atguigu.yygh.order.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;

import java.util.List;


/**
 * @author 用户名
 * @Type DictServiceImpl.java
 * @Desc
 * @date 2023/7/8 18:33
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public List<Dict> findChildData(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Dict> dicts = baseMapper.selectList(wrapper);
        for (Dict dict : dicts) {
            boolean hasChild = HasChild(dict.getId());
            dict.setHasChildren(hasChild);
            System.out.println("打印");
            System.out.println(dict);

        }
        return dicts;
    }
    /**判断是否有孩子节点
     * **/
    public boolean HasChild(Long id){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Integer count = baseMapper.selectCount(wrapper);

        return count>0;
    }
    /**把数据库的数据导出为excel表格
     * **/
    @Override
    public void exportData(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("数据字典", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        List<Dict> dicts = baseMapper.selectList(null);
        List<DictEeVo> objects = new ArrayList<>(dicts.size());
        //拷贝属性到其他对象
        for (Dict object : dicts) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(object,dictEeVo);
            objects.add(dictEeVo);
        }
        EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet("数据字典")
                .doWrite(objects);



    }
    @CacheEvict(value = "dict", allEntries=true)
    @Override
    public void importData(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper)).sheet().doRead();
    }
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public String getNameByValueAndParentId(Integer Hostype, Integer parentId) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId)
                .eq("value",Hostype);
        Dict dict = baseMapper.selectOne(wrapper);
        return dict.getName();
    }
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public String getNameByValueAndParentIdByString(String Hostype, String parentId) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId)
                .eq("value",Hostype);
        Dict dict = baseMapper.selectOne(wrapper);
        return dict.getName();
    }

    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public String getDetailAddress(Integer provinceCode, Integer cityCode,Integer districtCode) {
        //先得到省份
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("value",provinceCode);
        String provinceName = baseMapper.selectOne(wrapper).getName();
        //再得到城市名
        QueryWrapper<Dict> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id",provinceCode)
                .eq("value",cityCode)
        ;
        String districtName = baseMapper.selectOne(wrapper1).getName();
        //再得到区
        QueryWrapper<Dict> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("parent_id",cityCode)
                .eq("value",districtCode)
        ;
        String cityName = baseMapper.selectOne(wrapper2).getName();

        if(StringUtils.isEmpty(provinceCode) || StringUtils.isEmpty(provinceCode)){
            throw new HospitalException(ResultCodeEnum.DATA_ERROR);
        }


        return provinceName+districtName+cityName;
    }
    //根据parent,id得到name
    @Override
    public List<Dict> getNamesBydictCode(Integer parent) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parent);

        List<Dict> dicts = baseMapper.selectList(wrapper);


        return dicts;
    }
    //根据dtcode得到名字
    @Override
    public List<Dict> getNamesBydictCode1(String code) {
        //得到parentId
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code",code);
        Dict dict = baseMapper.selectOne(wrapper);
        Long parentId = dict.getId();
        //long转integer
        Integer integer = Integer.valueOf(parentId.toString());
        List<Dict> dicts = this.getNamesBydictCode(integer);


        return dicts;
    }


    @Override
    public List<String> getNamesByChildDictCode(Integer parent) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parent);

        List<Dict> dicts = baseMapper.selectList(wrapper);
        ArrayList<String> strings = new ArrayList<>();


        return strings;
    }

    @Override
    public List<String> getNamesByChildDictCode1(Integer parent) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parent);

        Dict dict = baseMapper.selectOne(wrapper);
        String value = dict.getValue();
        QueryWrapper<Dict> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id",value);

        List<Dict> dicts = baseMapper.selectList(wrapper1);
        List<String> strings = new ArrayList<>();
        for (Dict dict1 : dicts) {
            strings.add(dict1.getName());
        }


        return strings;

    }

    @Override
    public String getNameByValue(Integer value) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("value",value);
            Dict dict = baseMapper.selectOne(wrapper);
        return dict.getName();
    }


    //2023 8/23 18:06
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public String getNameByValueAndDicCode(String Hostype, String DicCode) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code",DicCode)
                .eq("value",Hostype);
        Dict dict = baseMapper.selectOne(wrapper);
        return dict.getName();
    }

    @Override
    public String getNameByStringValue(String value) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("value",value);
        Dict dict = baseMapper.selectOne(wrapper);

        return dict.getName();
    }



}


/**
 * Revision history
 * -------------------------------------------------------------------------
 * <p>
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2023/7/8 用户名 create
 */ 
